// pages/login-1/index.js
const getChina = require('../../../utils/region.js').getChina;
const api = require('../../../utils/api.js').apis;
// console.log(getChina('Shenzhen'))
const toDate = require('../../../utils/tool').toDate;
const oss = require('../../../utils/oss.js')
const mWx = require('../../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:'',
    avatarUrl:'',
    userInfo:{
      nickName:'',
      avatarUrl:'',
    }
  },
  btn(){
    
    if(this.data.userInfo.nickName){
      
      if(this.data.avatarUrl){
        this.setData({
          'userInfo.nickName':this.data.nickName,
          'userInfo.avatarUrl':this.data.avatarUrl,
        })
      } else{
        this.setData({
          'userInfo.nickName':this.data.nickName,
        })
      }
      this.setData({
        'userInfo.nickName':this.data.nickName,
        'userInfo.avatarUrl':img,
      })
      wx.setStorageSync('userInfo', this.data.userInfo)

      wx.navigateTo({
        url: '/login/pages/login-3/index',
      })
    }else{
      wx.showToast({
        title: '名字不能为空',
        icon: 'none'
      })
    }
    
  },
  bindblur(e){
    var value = e.detail.value
    console.log(value)
    this.setData({
      nickName:value
    })
  },
  // 图片上传未写完
  changeAvatar: function () {
    var that = this;
    wx.chooseImage({
      count: 1, //最多可以选择的图片总数
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        api.getStsOss({}).then(res=>{
          // resolve(res)
          console.log(res)
          if (res.success) {
            // console.log(res.dadta.success)
            that.upLoadFileService(tempFilePaths, res.data)
          }
        })
        that.queryOssData().then(res => {
          console.log(res)
          
        })
      }
    });
  },
  // 更改图片路径
  upLoadFileService(file, ossData) {
    console.log(file,ossData)
    var arrurl = []
    const that = this;
    const date = new Date()
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let obj = oss.util.aliOssParams(ossData.tempAk, ossData.tempSk,'https://xuedaozaixain.oss-cn-beijing.aliyuncs.com');
    let alikey = `user/${year}${month}${day}/user_head_${date.getTime()}_${Math.round(Math.random() * 100)}.${file[0].split('.').pop()}`;
    wx.uploadFile({
      url: 'https://xuedaozaixain.oss-cn-beijing.aliyuncs.com',
      filePath: file[0],
      name: 'file',
      formData: {
        key: alikey,
        name: file[0],
        policy: obj.policy,
        OSSAccessKeyId: ossData.tempAk,
        success_action_status: '200',
        signature: obj.signature,
        'x-oss-security-token': ossData.token
      },
      success: (res) => {
        console.log("https://xuedaozaixain.oss-cn-beijing.aliyuncs.com" + "/" + alikey)

        that.setData({
          avatarUrl: 'https://xuedaozaixain.oss-cn-beijing.aliyuncs.com' + "/" + alikey
        })
      },
      fail: function(res) {
        console.log(res)
        wx.hideToast();
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function(res) {}
        })
      }
    });
  },
  // 获取oss
  queryOssData() {
    return new Promise((resolve, reject) => {
      console.log(resolve)
      api.getStsOss({}).then(res=>{
        // resolve(res)
        console.log(res)
      })
      // mWx.request({
      //   url: getStsOss,
      //   success: (res) => {
      //     resolve(res)
      //   }
      // })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('userInfo')
    this.setData({
      userInfo:user
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})